package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {
    @Reference
    MenuService menuService;
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        log.debug("PermissionController{" + queryPageBean + "}");
        PageResult pageResult = menuService.findByPage(queryPageBean);
        log.debug("菜单分页查询成功");
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, pageResult);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu, Integer Id) {
        if (Id == 999999) {
            menu.setParentMenuId(null);
        } else {
            menu.setParentMenuId(Id);
        }
        log.debug("MenuController:{" + menu + "}");
        menuService.add(menu);
        log.debug("添加菜单成功");
        return new Result(true, MessageConst.ADD_MENU_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.debug("controller -- findById: {" + id + "}");
        Menu menu = menuService.findById(id);
        log.debug("菜单根据id查询：{" + menu + "}");
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menu);
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        log.debug("MenuController--edit:" + menu + "}");
        menuService.edit(menu);
        log.debug("菜单编辑成功");
        return new Result(true, MessageConst.EDIT_MENU_SUCCESS);
    }
    @RequestMapping("/delById")
    public Result delById(Integer id) {
        log.debug("MenuController--delete;{" + id + "}");
        menuService.delById(id);
        log.debug("菜单删除成功");
        return new Result(true, MessageConst.DELETE_MENU_SUCCESS);
    }
    //查询所有菜单列表
    @RequestMapping("/findAll")
    public Result findAll() {
        log.debug("MenuController--findAll");
        List<Menu> list =menuService.findAll();
        log.debug("查询成功");
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS,list);
    }
    //用于查找所有的自关联的id
    @RequestMapping("/findMenuparentMenuId")
    public Result findMenuparentMenuId() {
        try {
            List<Map> data = menuService.findMenuparentMenuId();
            return new Result(true, MessageConst.QUERY_MENU_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_MENU_FAIL);
        }
    }




    @RequestMapping("/showUserMenu")
    public Result showUserMenu(String username) {
        log.debug(username);
        //从数据库中查询的菜单集合
        List<Menu> list = menuService.showUserMenu(username);
        System.out.println(list);
        //返回页面的结果
        List<Map<String, Object>> menuList = new ArrayList<>();
        //固定的工作台map
        Map<String, Object> map = new HashMap<>();
        map.put("path", "1");
        map.put("title", "工作台");
        map.put("icon", "fa-dashboard");
        menuList.add(map);
        //遍历从数据库中查询的菜单集合，将其添加进listMap
        for (Menu menu : list) {
            putMenuList(menuList, menu);
        }
        return new Result(true, MessageConst.GET_MENU_SUCCESS, menuList);
    }

    public void putMenuList(List<Map<String, Object>> list, Menu menu) {
        Map<String, Object> map = new HashMap<>();
        map.put("path", menu.getPath());
        map.put("title", menu.getName());
        map.put("linkUrl", menu.getLinkUrl());
        map.put("icon", menu.getIcon());
        if (menu.getChildren() != null) {
            List<Map<String, Object>> listChildren = new ArrayList<>();
            for (Menu child : menu.getChildren()) {
				putMenuList(listChildren, child);
			}
            map.put("children", listChildren);
        }
        list.add(map);
    }

}




